##########################################
## Copyright 2012-2013 Ceruti Francesco & contributors
##
## This file is part of LiSP (Linux Show Player).
##########################################

from PyQt4 import QtCore
from PyQt4 import QtGui
from lisp.ui.preferences.preferences_widget import PreferencesWidget


class Equalizer(PreferencesWidget):

    NAME = 'Equalizer'

    FREQ = ['29Hz', '59Hz', '119Hz', '237Hz', '474Hz', '947Hz', '1889Hz', '3770Hz', '7523Hz', '15011Hz']

    def __init__(self, size, parent=None):
        super(Equalizer, self).__init__(size, parent)

        self.groupBox = QtGui.QGroupBox(self)
        self.groupBox.setGeometry(self.geometry())
        self.groupBox.setTitle('10 Bands Equalizer (IIR)')

        self.gridLayout = QtGui.QGridLayout(self.groupBox)

        self.sliders = {}

        for n in range(10):
            fLabel = QtGui.QLabel(self.groupBox)
            fLabel.setText(self.FREQ[n])
            self.gridLayout.addWidget(fLabel, n, 0)

            horizontalSlider = QtGui.QSlider(self.groupBox)
            horizontalSlider.setMinimum(-24)
            horizontalSlider.setMaximum(12)
            horizontalSlider.setPageStep(1)
            horizontalSlider.setProperty("value", 0)
            horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
            self.gridLayout.addWidget(horizontalSlider, n, 1)

            self.sliders['band' + str(n)] = horizontalSlider

            label = QtGui.QLabel(self.groupBox)
            label.setAlignment(QtCore.Qt.AlignCenter)
            label.setNum(0)
            self.gridLayout.addWidget(label, n, 2)

            horizontalSlider.valueChanged.connect(label.setNum)

        self.gridLayout.setColumnStretch(0, 1)
        self.gridLayout.setColumnStretch(1, 10)
        self.gridLayout.setColumnStretch(2, 1)

    def enableCheck(self, enable):

        self.groupBox.setCheckable(enable)
        self.groupBox.setChecked(False)

    def getConfiguration(self):
        conf = {}
        if(self.groupBox.isCheckable() and not self.groupBox.isChecked()):
            conf['equalizer'] = {}
            for band in self.sliders:
                conf['equalizer'][band] = self.sliders[band].value()
        return conf

    def setConfiguration(self, conf):
        if(conf is not None):
            if('equalizer' in conf):
                if(type(conf['equalizer']) == type({})):
                    for band in self.sliders:
                        if(band in conf['equalizer']):
                            self.sliders[band].setValue(conf['equalizer'][band])
                        else:
                            self.sliders[band].setEnabled(False)
                else:
                    self.setEnabled(False)
            else:
                self.setEnabled(False)
